package com.frain.springcloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * 
 * @ClassName: RibbonController 
 * @Description: 从配置文件的ribbon的配置文件中获取到实例。然后不用eureka来进行管理负载均衡。实现分流操作
 * @author lenovo
 * @date 2018年8月5日 下午5:44:10 
 *
 */
@RestController
@RequestMapping("/ribbon")
public class RibbonController {

    /*@Autowired
    private RibbonProvider ribbonProvider;*/

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @RequestMapping("/index")
    @ResponseBody
    public String getIndex() {

        ServiceInstance serviceInstance = this.loadBalancerClient.choose("service-by-properties");
        System.out.println("========= " + serviceInstance.getHost() + ":" + serviceInstance.getPort() + ":"
                + serviceInstance.getServiceId());
        System.out.println(serviceInstance);
        String path = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort()
                + "/springapi/testApi/getString";
        String string = this.restTemplate.getForObject(path, String.class);
        System.out.println("path=====" + path);
        System.out.println(string);
        // http://localhost:7900/simple/
        // VIP: Virtual IP http://microservice-provider-user/ 即虚拟IP 服务提供者的ServiceId （spring.application.name）
        // return this.restTemplate.getForObject("http://service-by-properties/testApi/getString", String.class);

        return string;
    }

}
